import { Component, OnInit } from '@angular/core';
import {PersonalMessage} from "../entity/PersonalMessage";
import {UserService} from "../service/user.service";
import {CommonIp} from "../entity/Ip";
import {ActivatedRoute, Params, Router} from "@angular/router";

@Component({
  selector: 'personalbasic-message',
  templateUrl: './personal-message.html',

})
export class PersonalMessageComponent extends CommonIp implements OnInit {
  public personalmessage:PersonalMessage;

  constructor(private userservice:UserService,
              private activatedRouter: ActivatedRoute,
              public router:Router) {
    super();
  }

  private getPersonalBaseMessage(userid:string){
    this.userservice.getPersonalMessage(userid)
      .then(personalmessage=>this.personalmessage=personalmessage)
  }
  private initParam(param: any):Promise<string> {
    this.getPersonalBaseMessage(param);

    return param;
  }
  ngOnInit() {

    // if(sessionStorage.getItem("id")!=null){
    //   this.getPersonalBaseMessage(sessionStorage.getItem("id"));
    // }
    // this.activatedRouter.params
    //   .switchMap((params: Params) =>this.initParam(params["id"])).subscribe();
    //
    // if(localStorage.getItem("userimg")!=null){
    //
    //   document.getElementById("imgHeads").setAttribute("src",localStorage.getItem("userimg"));
    //
    // }
    // ;
// <<<<<<< HEAD
//     if(sessionStorage.getItem("isclick")=="true"){
//       this.getPersonalBaseMessage(sessionStorage.getItem("rankingid"));
//       sessionStorage.removeItem("isclick");
//       if (sessionStorage.getItem("rankingimg")!=null){
//         document.getElementById("imgHeads").setAttribute("src",sessionStorage.getItem("rankingimg"));
//         sessionStorage.removeItem("rankingimg");
//       }
//     }else {
//       this.getPersonalBaseMessage(sessionStorage.getItem("id"));
//
//       if(sessionStorage.getItem("userimg")!=null){
//         document.getElementById("imgHeads").setAttribute("src",sessionStorage.getItem("userimg"));
//
//     }
//
//
//
//     }
// =======


    this.activatedRouter.params
    // (+) converts string 'id' to a number
      .switchMap((params: Params) =>
        this.userservice.getPersonalMessage(this.initMethod()))
      .subscribe(personalmessage=>{

        this.personalmessage=personalmessage;
        // sessionStorage.setItem("userimg",this.userimgIp+personalmessage.photo);

        document.getElementById("imgHeads").setAttribute("src",this.userimgIp+personalmessage.photo);
      });

  }
 private initMethod(){

   document.getElementById("join").removeAttribute("class");
   document.getElementById("follow").setAttribute("class","active");
   this.router.navigate(['/personal/personalFollow/',{id:sessionStorage.getItem("personInfoId"), pageNo: 0}]);
  return sessionStorage.getItem("personInfoId");
 }

}
